﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using C1.Win.C1Command;

namespace Demo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            Rectangle rect = Screen.GetWorkingArea(this);
            this.Width = rect.Width;
            this.Height = rect.Height;
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            BindTreeView();
        }

        /// <summary>
        /// 绑定TreeView数据
        /// </summary>
        public void BindTreeView()
        {
            TreeNode tmain = new TreeNode();
            tmain.Text = "基础档案";
            tvMenu.Nodes.Add(tmain);

            TreeNode tDepartment = new TreeNode();
            tDepartment.Text = "部门管理";

            TreeNode tEmployee = new TreeNode();
            tEmployee.Text = "员工管理";

            tmain.Nodes.Add(tDepartment);
            tmain.Nodes.Add(tEmployee);
        }

        /// <summary>
        /// 双击Treeview的菜单节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvMenu_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Text == "部门管理")
            {
                C1DockingTabPage tabpage = FindFormOpen("FrmDeparment");
                if (tabpage != null)
                {
                    this.c1DockingTab1.SelectedTab = tabpage;
                }
                else
                {
                    AddFormToTabPage(new FrmDeparment());
                }
            }
            else if (e.Node.Text == "员工管理")
            {
                
                C1DockingTabPage tabpage = FindFormOpen("FrmEmployee");
                if (tabpage != null)
                {
                    this.c1DockingTab1.SelectedTab = tabpage;
                }
                else
                {
                    AddFormToTabPage(new FrmEmployee());
                }
            }
        }

        /// <summary>
        /// 查找页面是否已经打开
        /// </summary>
        /// <param name="formName"></param>
        /// <returns></returns>
        private C1DockingTabPage FindFormOpen(string formName)
        {
            foreach (C1DockingTabPage page in c1DockingTab1.TabPages)
            {
                if (page.Name == formName)
                    return page;
            }
            return null;
        }

        /// <summary>
        /// 添加页面到tabpage
        /// </summary>
        /// <param name="form"></param>
        private void AddFormToTabPage(Form form)
        {
            if (form != null)
            {
                form.TopLevel = false;
                form.FormBorderStyle = FormBorderStyle.None;
                C1DockingTabPage page = new C1DockingTabPage();
                page.Text = form.Text;
                page.Name = form.Name;
                page.Font = this.Font;
                form.Parent = page;
                form.Dock = DockStyle.Fill;
                form.FormBorderStyle = FormBorderStyle.None;
                form.AutoScroll = true;
                form.FormClosed += new FormClosedEventHandler(form_FormClosed);
                page.Controls.Add(form);
                c1DockingTab1.TabPages.Add(page);
                c1DockingTab1.SelectedTab = page;
                form.Show();
            }
        }

        /// <summary>
        /// 关闭页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form f = sender as Form;
            if (f != null)
            {
                if (c1DockingTab1.TabPages.Contains(f.Parent))
                {
                    c1DockingTab1.TabPages.Remove(f.Parent);
                }
            }
        }

    }
}
